package com.xgq.drink.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xgq.drink.entity.DiscountClaim;
import com.xgq.drink.entity.DiscountInfo;
import com.xgq.drink.mapper.DiscountClaimMapper;
import com.xgq.drink.mapper.DiscountInfoMapper;
import com.xgq.drink.service.IDiscountClaimService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DiscountClaimServiceImpl extends ServiceImpl<DiscountClaimMapper, DiscountClaim> implements IDiscountClaimService {
    @Autowired
    private DiscountInfoMapper discountInfoMapper;
    @Override
    public IPage<DiscountClaim> getDiscountClaimsByUserId(Long userId, String discountType, Integer status, int current, int size) {
        System.out.println("=================1");
        System.out.println(userId);
        System.out.println(discountType);
        System.out.println(status);
        Page<DiscountClaim> page = new Page<>(current, size);
        QueryWrapper<DiscountClaim> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        System.out.println("=================2");
        if (status != null) {
            queryWrapper.eq("status", status);
        }
        System.out.println("=================3");
        // 查询分页结果
        //        System.out.println(resultPage);
//        System.out.println("=================4");
//        // 手动填充优惠信息，并根据 discountType 过滤
//        List<DiscountClaim> filteredClaims = resultPage.getRecords().stream()
//                .filter(claim -> {
//                    if (claim.getDiscountId() != null) {
//                        DiscountInfo discountInfo = discountInfoMapper.selectById(claim.getDiscountId());
//                        claim.setDiscountInfo(discountInfo);
//                        return discountInfo != null && (discountType == null || discountType.equals(discountInfo.getDiscountType()));
//                    }
//                    return false;
//                })
//                .toList();
//
//        System.out.println("=================5");
//        resultPage.setRecords(filteredClaims);
//        System.out.println(resultPage);
        return this.page(page, queryWrapper);
    }

    @Override
    public void markAsUsed(Long claimId) {
        DiscountClaim claim = this.getById(claimId);
        if (claim != null) {
            claim.setStatus(2); // 设置为已使用
            this.updateById(claim);
        }
    }

    @Override
    public void markAsExpired(Long claimId) {
        DiscountClaim claim = this.getById(claimId);
        if (claim != null) {
            claim.setStatus(3); // 设置为已失效
            this.updateById(claim);
        }
    }

    @Override
    public DiscountClaim claimDiscount(Long userId, Long discountId) {
        DiscountClaim claim = new DiscountClaim();
        claim.setUserId(userId);
        claim.setDiscountId(discountId);
        claim.setStatus(1); // 设置为待使用
        this.save(claim);
        return claim;
    }
}